package jee.boot.module.jasper.service;

import java.io.File;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;

import jee.boot.module.jasper.iface.JasperQueryDataInterface;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Component
public class JasperReportsService {

	private static Logger logger = LoggerFactory.getLogger(JasperReportsService.class);

	@Autowired
	private DruidDataSource dataSource;

	private void setDownloadName(String fileName, String fileType, HttpServletResponse response) {
		try {
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition",
					"inline;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1") + "." + fileType);
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	/**
	 * @param jasperName
	 * @param isPreview
	 * @param response
	 * @param list
	 */
	@SuppressWarnings("rawtypes")
	public void exportPdf(String jasperName, boolean isPreview, HttpServletResponse response, List list) {
		this.exportPdf(jasperName, isPreview, response, list, null);
	}

	/**
	 * @param jasperName
	 * @param isPreview
	 * @param response
	 * @param jasperQueryDataInterface
	 */
	public void exportPdf(String jasperName, boolean isPreview, HttpServletResponse response,
			JasperQueryDataInterface<?> jasperQueryDataInterface) {
		this.exportPdf(jasperName, isPreview, response, jasperQueryDataInterface, null);
	}

	/**
	 * @param jasperName
	 * @param isPreview
	 * @param response
	 * @param jasperQueryDataInterface
	 * @param params
	 */
	public void exportPdf(String jasperName, boolean isPreview, HttpServletResponse response,
			JasperQueryDataInterface<?> jasperQueryDataInterface, Map<String, Object> params) {
		jasperName = this.getJasperClassPath() + jasperName;
		try {
			JRDataSource jrDataSource = new JRBeanCollectionDataSource(jasperQueryDataInterface.getJasperData());
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperName, params, jrDataSource);
			if (isPreview) {
				setDownloadName(jasperPrint.getName(), "pdf", response);
			}

			JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	/**
	 * @param jasperName
	 * @param isPreview
	 * @param response
	 * @param list
	 * @param params
	 */
	public void exportPdf(String jasperName, boolean isPreview, HttpServletResponse response,
			@SuppressWarnings("rawtypes") List list, Map<String, Object> params) {
		jasperName = this.getJasperClassPath() + jasperName;
		try {
			JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperName, params, jrDataSource);
			if (isPreview) {
				setDownloadName(jasperPrint.getName(), "pdf", response);
			}
			JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public void exportPdf(String jasperName, boolean isPreview, HttpServletResponse response,
			Map<String, Object> params) {
		jasperName = this.getJasperClassPath() + jasperName;
		try {
			Connection connection = dataSource.getConnection();
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperName, params, connection);
			if (isPreview) {
				setDownloadName(jasperPrint.getName(), "pdf", response);
			}
			JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public String getJasperClassPath() {

		return this.getClass().getClassLoader().getResource("").getPath() + File.separator + "templates"
				+ File.separator + "jasper" + File.separator;
	}
}
